package com.mysql.transaction.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import redis.clients.jedis.JedisPool;

import java.time.Duration;
import java.util.Map;

/**
 * @author 小木
 * @date 2021/7/6
 */
 @EnableCaching
 @Configuration
 public class RedisConfig {

     // ------------------------------------------ [单节点]
     @Bean
     public JedisPool jedisPoolFactory(RedisProperties redisProperties) {
         GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
         // 获取[配置文件]中的配置信息
         RedisProperties.Pool pool = redisProperties.getJedis().getPool();
         poolConfig.setMaxIdle(pool.getMaxIdle());
         poolConfig.setMaxTotal(pool.getMaxActive());
         poolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis());

         JedisPool jp = new JedisPool(poolConfig,
                 redisProperties.getHost(),
                 redisProperties.getPort(),
                 (int) (redisProperties.getTimeout().getSeconds() * 1000),
                 StringUtils.isEmpty(redisProperties.getPassword()) ? null : redisProperties.getPassword(),
                 redisProperties.getDatabase());
         return jp;
     }




 }
